package com.anuprayog.flyinghelicopter;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author amit This is the activity where the game play takes place. This has
 *         the game panel set as its view instead of any XML files
 */
public class GameActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// preventing game screen from going into sleep
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		// requesting to turn the title OFF
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// making it full screen
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// set our MainGamePanel as the View
		setContentView(new MainGamePanel(this));
		// have volume buttons control game sounds
		setVolumeControlStream(AudioManager.STREAM_MUSIC);

	}

	// Handle pause events
	@Override
	protected void onPause() {
		MainThread.running = false;// this is the value for stop the loop in the
									// run()
		MainThread.pauser = true;// used for updating the pause button in the
									// game panel
		super.onPause();
	}
	
	/*@Override
	protected void onResume() {
		MainThread.running = true;// this is the value for stop the loop in the
									// run()
		MainThread.pauser = false;// used for updating the pause button in the
									// game panel
		super.onResume();
	}*/

	@Override
	public void onBackPressed() {

		MainThread.running = false;
		MainThread.pauser = true;
		final Intent intent = new Intent(this, MainActivity.class);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		builder.setMessage("Do you want to quit?");
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// if user pressed "yes", then he is allowed to exit from
				// application
				startActivity(intent);
				finish();
			}
		});
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// if user select "No", just cancel this dialog and continue
				// with app
				dialog.cancel();
				// MainThread.running=false;
				// MainThread.pauser=true;
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

	}
}
